/*
 * Copyright (c) 2017. 深圳一七网络科技有限公司. All rights reserved.
 */

package com.yiqi.zhiyuan.framework;

import android.content.Context;
import android.text.TextUtils;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.suozhang.framework.component.http.OkHttpManager;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.DynamicTimeFormat;
import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.common.romgim.SealAppContext;
import com.yiqi.zhiyuan.common.utils.AppUtil;

import java.util.concurrent.TimeUnit;

import cn.jpush.android.api.JPushInterface;
import io.rong.imkit.RongContext;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;

/**
 * @author lijuwen
 * @e-mail 1440919311@qq.com
 * @date 2017/3/27 14:15
 */

public class LibManager {

    public void init(Context app) {
        //初始化第三方库
        initJPush(app);
        //其他库...
        initSmartRefreshLayout();
        initRongIm(app);

    }


//    /**
//     * 友盟分享
//     */
//    private void initUM(Context app) {
//        //init config 在使用RxWebSocket之前设置即可，推荐在application里初始化
//        Config config = new Config.Builder()
//                .setShowLog(true)           //show  log
//                .setClient(new OkHttpManager().buildOkHttpClient())   //if you want to set your okhttpClient
//                .setShowLog(true, "RX_WEBSOCKET")
//                .setReconnectInterval(2, TimeUnit.SECONDS)  //set reconnect interval
//                .build();
//        RxWebSocket.setConfig(config);
//    }

    /**
     * 极光推送
     */
    private void initJPush(Context app) {
        // 设置开启日志,发布时请关闭日志
        JPushInterface.setDebugMode(AM.isDebug());
        // 初始化 JPush
        JPushInterface.init(app);
        JPushInterface.stopCrashHandler(app);

    }
    private  void initSmartRefreshLayout() {
        //static 代码段可以防止内存泄露

        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                //全局设置主题颜色
                layout.setPrimaryColorsId(R.color.primary_comparison, R.color.primary_highlight);
                layout.setReboundDuration(100);
                //指定为经典Header，默认是 贝塞尔雷达Header
                return new ClassicsHeader(context)
                        .setTimeFormat(new DynamicTimeFormat("更新于 %s"));
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(20)
                        .setAccentColorId(R.color.primary_highlight);
                //  return new BallPulseFooter(context);
            }
        });

//        AM.api().newRetrofit()
    }
    private static  void initRongIm(Context context){
        /**
         *
         * OnCreate 会被多个进程重入，这段保护代码，确保只有您需要使用 RongIM 的进程和 Push 进程执行了 init。
         * io.rong.push 为融云 push 进程名称，不可修改。
         */
        if (context.getApplicationInfo().packageName.equals(AppUtil.getCurProcessName(context.getApplicationContext()))) {
//            RongPushClient.registerHWPush(context);
//            RongPushClient.registerMiPush(context, "2882303761517873170", "5681787336170");
//            RongPushClient.registerMZPush(context, "1002097", "d4b897f7955a426a9f46c31725db7dc5");
//            try {
//                RongPushClient.registerFCM(context);
//            } catch (RongException e) {
//                e.printStackTrace();
//            }
            /**
             * IMKit SDK调用第一步 初始化
             */
            RongIM.init(context);
            RongContext.init(context);
            SealAppContext.init(context);

            RongIM.setConnectionStatusListener(new RongIMClient.ConnectionStatusListener() {
                @Override
                public void onChanged(RongIMClient.ConnectionStatusListener.ConnectionStatus status) {
                    if (status == ConnectionStatus.TOKEN_INCORRECT) {
                        if (AM.user().isLogin()){
                            if (!TextUtils.isEmpty(ZYConstants.RONG_TOKEN)) {
                                RongIM.connect(ZYConstants.RONG_TOKEN, SealAppContext.getInstance().getConnectCallback());
                            } else {
                                Logger.e("seal", "token is empty, can not reconnect");
                            }
                        }else {
                            Logger.e("seal", "token is empty, can not reconnect");
                        }

                    }
                }
            });
        }


    }
}
